package com.etc.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.etc.dto.Result;
import com.etc.entity.Discuss;
import com.etc.entity.Games;
import com.etc.service.impl.DiscussServiceImpl;
import com.etc.service.impl.GamesServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class DiscussServlet
 */
@WebServlet("/DiscussServlet")
public class DiscussServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DiscussServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String op = request.getParameter("op");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = "";
		Result result = new Result();
		if("find".equals(op)) {
			String gamesId = request.getParameter("gamesId");
			List<Discuss> findAllDiscussByGamesId = new DiscussServiceImpl().findAllDiscussByGamesId(Integer.valueOf(gamesId));
			if(findAllDiscussByGamesId!=null) {
				result.ok(findAllDiscussByGamesId);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
		}else if ("delete".equals(op)) {
			boolean flag = true;
			String discussIdList = request.getParameter("discussIdList");
			String[] discussIdArray = discussIdList.split(",");
			for (String str : discussIdArray) {
				Integer discussId = Integer.valueOf(str);
				boolean deleteDiscuss = new DiscussServiceImpl().deleteDiscuss(discussId);
				if(!deleteDiscuss) {
					flag = false;
				}
			}
			if(flag) {
				result.ok("删除成功");
			}else {
				result.fail(0, "删除失败");
			}
			json = objectMapper.writeValueAsString(result);
		}else if ("add".equals(op)) {
			String gamesId = request.getParameter("gamesId");
			String customerId = request.getParameter("customerId");
			String content = request.getParameter("content");
			String com = request.getParameter("comment");
			String id = request.getParameter("replyId");
			
			Integer comment = null;
			if(com!=null&&!"".equals(com)) {
				comment = Integer.valueOf(com);
			}
			
			Integer replyId = null;
			if(id!=null&&!"".equals(id)) {
				replyId = Integer.valueOf(id);
			}
			
			Discuss discuss = new Discuss();
			discuss.setGamesId(Integer.valueOf(gamesId));
			discuss.setCustomerId(Integer.valueOf(customerId));
			discuss.setContent(content);
			discuss.setReplyId(replyId);
			discuss.setComment(comment);
			
			
			boolean addDiscuss = new DiscussServiceImpl().addDiscuss(discuss);
			
			if (addDiscuss) {
				result.ok("添加成功");
			}else {
				result.fail(0, "添加失败");
			}
			json = objectMapper.writeValueAsString(result);
			
		}
		
		
		
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
	}

}
